package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping("/listBusinessByOrderTypeId")
    public List<Business> listBusinessByOrderTypeId(@RequestParam Integer orderTypeId) throws Exception {
        return businessService.listBusinessByOrderTypeId(orderTypeId);
    }

    @GetMapping("/{businessId}")
    public ResponseEntity<?> getBusinessById(@PathVariable Integer businessId) {
        if (businessId == null) {
            return ResponseEntity.badRequest().body("businessId 不能为空");
        }
        Business b = businessService.getBusinessById(businessId);
        return b != null ? ResponseEntity.ok(b)
                         : ResponseEntity.status(HttpStatus.NOT_FOUND).body("商家不存在");
    }

    @GetMapping("/me")
    public ResponseEntity<?> getCurrentBusiness(HttpSession session) {
        Long userIdLong = (Long) session.getAttribute("userId");
        String userId = userIdLong != null ? String.valueOf(userIdLong) : null;
        Business currentBusiness = businessService.getCurrentBusiness(userId);
        return currentBusiness != null
                ? ResponseEntity.ok(currentBusiness)
                : ResponseEntity.status(HttpStatus.NOT_FOUND).body("商家信息未找到");
    }
}